package com.lkm.app_ui.ui.second;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Looper;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.lkm.app_ui.MBaseFragment;
import com.lkm.app_ui.MyApplication;
import com.lkm.app_ui.R;
import com.lkm.app_ui.help.QRCodeUtil;
import com.lkm.app_ui.ui.SalePrint;
import com.lkm.comlib.Config;
import com.lkm.frame.task2.CTaskExecutor;
import com.lkm.frame.task2.TaskAdapter;
import com.lkm.javatool.lang.RunnablePR;

import androidx.annotation.Nullable;





public class PaySuccessTipsFragment  extends MBaseFragment {

    public static PaySuccessTipsFragment getInstance(String printTable,String orderurl) {
        PaySuccessTipsFragment f = new PaySuccessTipsFragment();
        Bundle data = new Bundle();
        data.putString("orderurl:s",orderurl);
        data.putString("printTable:s",printTable);
        f.setArguments(data);
        return f;
    }

    @Override
    protected boolean configIsLockKey() {
        return false;
    }

    @Override
    protected int configLayoutResID() {
        return R.layout.fragment_sec_pay_tips_success;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);







////                    getActivity().getSupportFragmentManager().popBackStackImmediate();

////                    getParentFragment().getChildFragmentManager().popBackStackImmediate();


//                ((DisplaySecondModule)MyApplication.getInstance().getModuleManager().loadModule(DisplaySecondModule.class)).dismiss(PaySuccessTipsFragment.this);



        CreateQR createQR=new CreateQR();
        createQR.setParam(new Object[]{getArguments().getString("orderurl:s")});
        createQR.action();
        createQR.execTask();

    }

    protected class CreateQR extends TaskAdapter<Object[], Void, Bitmap> {

        public CreateQR() {
            super(new CTaskExecutor<Object[], Void, Bitmap>(Config.getTaskExecutor(), Looper.getMainLooper()) , CreateQR.class.getName(), MyApplication.getInstance().getTaskManager2());
        }


        @Override
        public void doExecuting(Object[] param) {
            super.doExecuting(param);

            mTaskExecutor.executorDoAsyncExecuting(new RunnablePR<Object[], Void>() {
                @Override
                public Void run(Object[] objects) {
                    complete(onExecuting(objects));
                    return null;
                }
            },param);
        }

        public Bitmap onExecuting(Object[] param) {

            int ip = -1;
            String orderurl  = (String) param[++ip];
            Bitmap bitmap = QRCodeUtil.createImage(orderurl, 400, 400);
            return bitmap;//ResponEntity.fromJson(Api.order_add(c,datas,this),AddOrderResult.class);
        }

        @Override
        public void onFinish(Bitmap bitmap) {
            super.onFinish(bitmap);
            if(isExit())return;

            String printTable=getArguments().getString("printTable:s");




















            int printTableLength= printTable.length();
            StringBuilder stringBuilder=new StringBuilder();
            int count=0;
            for (int i = 0; i < printTableLength; i++) {
                String it=printTable.substring(i,i+1);
                count+= SalePrint.getCharWidth(it);
//                System.out.println(SaleFragment.getCharWidth(it)+"     "+it);
                stringBuilder.append(it);
                if(count>=32){
                    count=0;
                    stringBuilder.append("\n");
                }

            }
            String orderInfoStr=stringBuilder.toString();


            ImageView img_qr = (ImageView) findViewById(R.id.img_qr);
            img_qr.setImageBitmap(bitmap);
            ((TextView)findViewById(R.id.tv_printTable)).setText(orderInfoStr);

        }
    }
}
